﻿using System;
using System.Collections.Generic;
using TopBuilds.Restaurant.Data;

namespace TopBuilds.Restaurant.ManagementClient
{
    /// <summary>
    /// 禁止对象化，直接引用其 Instance
    /// </summary>

    public sealed class DepartmentCollection : System.Collections.ObjectModel.ObservableCollection<Department>
    {
        private string _condition;

        public string Condition
        {
            get { return _condition; }

            set
            {
                if (_condition != null && _condition == value)
                {
                    return;
                }

                _condition = value;

                if (Count > 0)
                {
                    base.Clear();
                }

                Department[] list = DepartmentObject.GetDepartments();
                if (list != null)
                {
                    foreach (Department item in list)
                    {
                        Add(item);
                    }
                }
            }
        }
        /// <summary>
        /// 初始化数据, 读入全部数据记录
        /// </summary>
        public DepartmentCollection()
        {
            Condition = null;
        }

        /// <summary>
        /// 收集满足条件的数据
        /// </summary>
        /// <param name="condition"></param>
        public DepartmentCollection(string condition)
        {
            Condition = condition;
        }

        /// <summary>
        /// 由Department Id 取得 Department
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public Department GetDepartment(int departmentId)
        {
            foreach (Department department in this)
            {
                if (department.Id == departmentId)
                {
                    return department;
                }
            }

            return null;
        }

    }
}
